package com.example.doctor.controller;
import com.example.common.Dto;
import com.example.common.DtoUtil;
import com.example.doctor.service.DoctorService;
import com.example.pojo.GdDoctor;
import com.example.pojo.GdUser;
import com.example.utils.ErrorCode;
import com.example.vo.*;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by LENOVO on 2018/9/23.
 */
@Controller
@RequestMapping("/api")
public class DoctorController {

    @Resource
    private DoctorService doctorService;

    /**
     * 查询医生详情
     * @param id
     * @return
     */
    @ApiOperation(value="查询医生详情信息",httpMethod = "GET", protocols = "HTTP", produces = "application/json", response = Dto.class,notes="查询医生详情信息")
    @RequestMapping(value = "/getDotorDetailsById/{id}", produces = "application/json", method = RequestMethod.GET)
    @ResponseBody
    public Dto getDotorDetailsById(@PathVariable String id)
    {

        DoctorDetailsReturnVo doctorDetailsReturnVo=null;
        try {
            doctorDetailsReturnVo=doctorService.getDotorDetailsById(id);
        }
        catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("查询医生详情信息失败！", ErrorCode.SEARCH_abnormal_FAILED);
        }
        if( doctorDetailsReturnVo !=null)
        {
            return DtoUtil.returnSuccess("查询医生详情信息成功!", doctorDetailsReturnVo);
        }
        else
        {
            return DtoUtil.returnSuccess("没有医生数据，请重新查询", doctorDetailsReturnVo);
        }
    }

    /**
     * 查询医生介绍
     * @param id
     * @return
     */
    @ApiOperation(value="查询医生介绍",httpMethod = "GET", protocols = "HTTP", produces = "application/json", response = Dto.class,notes="查询医生介绍")
    @RequestMapping(value = "/getDotorIntroduceById/{id}", produces = "application/json", method = RequestMethod.GET)
    @ResponseBody
    public Dto getDotorIntroduceById(@PathVariable String id)
    {

        DoctorIntroduceReturnVo doctorIntroduceReturnVo=null;
        try {
            doctorIntroduceReturnVo=doctorService.getDotorIntroduceById(id);
        }
        catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("询医生介绍失败！", ErrorCode.SEARCH_abnormal_FAILED);
        }
        if( doctorIntroduceReturnVo !=null)
        {
            return DtoUtil.returnSuccess("询医生介绍成功!", doctorIntroduceReturnVo);
        }
        else
        {
            return DtoUtil.returnSuccess("没有医生数据，请重新查询", doctorIntroduceReturnVo);
        }
    }

    /**
     * 关注医生
     * @param doctorId
     * @return
     */
    @ApiOperation(value="关注医生",httpMethod = "GET", protocols = "HTTP", produces = "application/json", response = Dto.class,notes="关注医生")
    @RequestMapping(value = "/attentionDoctor/{doctorId}", produces = "application/json", method = RequestMethod.GET)
    @ResponseBody
    public Dto attentionDoctor(@PathVariable String doctorId, HttpServletRequest request)
    {
        boolean result=false;

        try {
            result=doctorService.attentionDoctor(doctorId,request);
        }
        catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("关注医生失败！", ErrorCode.SEARCH_abnormal_FAILED);
        }
        if(result)
        {
            return DtoUtil.returnSuccess("关注医生成功!");
        }
        else
        {
            return DtoUtil.returnFail("关注医生失败！", ErrorCode.SEARCH_abnormal_FAILED);
        }
    }

    /**
     * 取消关注医生
     * @param doctorId
     * @return
     */
    @ApiOperation(value="关注医生",httpMethod = "GET", protocols = "HTTP", produces = "application/json", response = Dto.class,notes="关注医生")
    @RequestMapping(value = "/cancelAttentionDoctor/{doctorId}", produces = "application/json", method = RequestMethod.GET)
    @ResponseBody
    public Dto cancelAttentionDoctor(@PathVariable String doctorId,HttpServletRequest request)
    {
        boolean result=false;

        try {
            result=doctorService.cancelAttentionDoctor(doctorId,request);
        }
        catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("取消关注医生失败！", ErrorCode.SEARCH_abnormal_FAILED);
        }
        if(result)
        {
            return DtoUtil.returnSuccess("取消关注医生成功!");
        }
        else
        {
            return DtoUtil.returnFail("取消关注医生失败！", ErrorCode.SEARCH_abnormal_FAILED);
        }
    }

    /****
     * 发送下订单消息
     * @paramor derVo
     * @return
     */
    @RequestMapping(value = "/sendOrderMessage", produces = "application/json", method = RequestMethod.POST)
    @ResponseBody
    public Dto sendOrderMessage(OrderVo orderVo, HttpServletResponse response, HttpServletRequest request){
        Dto dto=null;
        try {
            response.setHeader("Access-Control-Allow-Origin", "*");
            dto=doctorService.sendOrderMessage(orderVo,request);
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("预订失败","0002");
        }
        return dto;
    }

    /**
     * 查询科室信息
     * @return
     */
    @ApiOperation(value="查询科室信息",httpMethod = "GET", protocols = "HTTP", produces = "application/json", response = Dto.class,notes="查询科室信息")
    @RequestMapping(value = "/getDeptInfo", produces = "application/json", method = RequestMethod.GET)
    @ResponseBody
    public Dto getDeptInfo(HttpServletResponse response)
    {
        response.setHeader("Access-Control-Allow-Origin", "*");
        List<DeptInfoVo> deptInfoVo=null;
        try {
            deptInfoVo=doctorService.getDeptInfo();
        }
        catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("查询科室信息失败！", ErrorCode.SEARCH_abnormal_FAILED);
        }
        if(deptInfoVo !=null)
        {
            return DtoUtil.returnSuccess("查询科室信息成功!",deptInfoVo);
        }
        else
        {
            return DtoUtil.returnSuccess("没有科室数据，请重新查询",deptInfoVo);
        }
    }

    /**
     * 推荐医生
     * @return
     */
    @ApiOperation(value="推荐医生",httpMethod = "GET", protocols = "HTTP", produces = "application/json", response = Dto.class,notes="查询科室信息")
    @RequestMapping(value = "/recommendDoctor", produces = "application/json", method = RequestMethod.GET)
    @ResponseBody
    public Dto recommendDoctor(HttpServletResponse response)
    {
        response.setHeader("Access-Control-Allow-Origin", "*");
        List<GdDoctor> doctorList=null;
        try {
            doctorList=doctorService.getRecommendDoctorList();
        }
        catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("查询推荐医生信息失败！", ErrorCode.SEARCH_abnormal_FAILED);
        }
        if(doctorList !=null)
        {
            return DtoUtil.returnSuccess("查询推荐医生信息成功!",doctorList);
        }
        else
        {
            return DtoUtil.returnSuccess("没有推荐医生数据，请重新查询",doctorList);
        }
    }

}
